package com.java110.mall.system.export.adapt;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.data.ExportDataDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.mall.system.export.IExportDataAdapt;
import com.java110.utils.util.BeanConvertUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("shopVipUser")
public class ExportShopVipUserAdapt implements IExportDataAdapt {

    private static final int MAX_ROW = 60000;

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;


    @Override
    public SXSSFWorkbook exportData(ExportDataDto exportDataDto) {
        SXSSFWorkbook workbook = null;  //工作簿
        //工作表
        workbook = new SXSSFWorkbook();
        workbook.setCompressTempFiles(false);

        Sheet sheet = workbook.createSheet("会员信息表");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("会员编号");
        row.createCell(1).setCellValue("会员名称");
        row.createCell(2).setCellValue("会员电话");
        row.createCell(3).setCellValue("性别");
        row.createCell(4).setCellValue("会员等级");
        row.createCell(5).setCellValue("会员生日");
        row.createCell(6).setCellValue("开始时间");
        row.createCell(7).setCellValue("结束时间");

        JSONObject reqJson = exportDataDto.getReqJson();
        ShopVipUserDto shopVipUserDto = BeanConvertUtil.covertBean(reqJson, ShopVipUserDto.class);

        //查询数据
        getReprotData(sheet, shopVipUserDto);

        return workbook;
    }

    private void getReprotData(Sheet sheet, ShopVipUserDto shopVipUserDto) {
        shopVipUserDto.setPage(1);
        shopVipUserDto.setRow(MAX_ROW);
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        setData(shopVipUserDtos, sheet, 0);
    }

    private void setData(List<ShopVipUserDto> shopVipUserDtos, Sheet sheet, int step) {
        Row row = null;
        ShopVipUserDto shopVipUserDto = null;
        for (int rowIndex = 0; rowIndex < shopVipUserDtos.size(); rowIndex++) {
            row = sheet.createRow(rowIndex + step + 1);
            shopVipUserDto = shopVipUserDtos.get(rowIndex);
            row.createCell(0).setCellValue(shopVipUserDto.getVipId());
            row.createCell(1).setCellValue(shopVipUserDto.getVipName());
            row.createCell(2).setCellValue(shopVipUserDto.getVipTel());
            row.createCell(3).setCellValue("0".equals(shopVipUserDto.getSex()) ? "女": "男");
            row.createCell(4).setCellValue(shopVipUserDto.getLevelName());
            row.createCell(5).setCellValue(shopVipUserDto.getBirthday());
            row.createCell(6).setCellValue(shopVipUserDto.getStartTime());
            row.createCell(7).setCellValue(shopVipUserDto.getEndTime());
        }
    }

}
